//
//  NestingTableViewCell.m
//  InduceTool
//
//  Created by 冯琰琰 on 2021/9/20.
//  Copyright © 2021 魏延龙. All rights reserved.
//

#import "NestingTableViewCell.h"
#import "NestingSubTVCell.h"
#import <Masonry/Masonry.h>
@interface NestingTableViewCell()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation NestingTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
        [self setupMonitor];
    }
    return self;
}

-(void)setupView {
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.text = @"测试测hi十四hi思思是好似是杀死ISIS红寺湖斯哈斯哈斯哈斯是是是是后四hi是四十岁了领导开饭啦看到了foe-思思是好似是杀死ISIS红寺湖斯哈斯哈斯哈斯是是是是后四hi是四十岁了领导开饭啦看到了foe";
    [self.contentView addSubview:self.contentLabel];

    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(20);
    }];
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [UIColor redColor];
    [self.tableView registerClass:[NestingSubTVCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 50.0;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [self.contentView addSubview:self.tableView];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-25);
        make.height.mas_equalTo(10);
    }];
}

- (void)setupMonitor {
    [self.tableView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    CGRect tableFrame = self.tableView.frame;
    tableFrame.size = self.tableView.contentSize;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentLabel.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.bottom.mas_equalTo(-25);
        make.height.mas_equalTo(tableFrame.size.height);
    }];
    [self.tableView.superview layoutIfNeeded];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NestingSubTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.contentView.backgroundColor = [UIColor redColor];
    return cell;
}

@end
